﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DECOR.BL.BE;
using System.Data.SqlClient;
using System.Data;

namespace DECOR.DL.DALC.MSSQL
{
    public class MaterialDALC : DALGC//, IMaterialDALC
    {
        #region IMaterialDALC Members

        public List<MaterialBE> listarMateriales()
        {
            List<MaterialBE> lsMaterial = new List<MaterialBE>();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from products", this.ConnectionString);
                DataSet ds = new DataSet();
                da.Fill(ds, "products");
                DataTable dt = ds.Tables[0];

                foreach (DataRow dr in dt.Rows)
                {
                    MaterialBE objMaterial = new MaterialBE();
                    objMaterial.CodMaterial = dr["ProductID"].ToString();
                    objMaterial.DescMaterial = dr["ProductName"].ToString();
                    objMaterial.PrecioUnit = Convert.ToDecimal(dr["UnitPrice"]);
                    objMaterial.CodUMedida = dr["QuantityPerUnit"].ToString();
                    //objMaterial. = dr[""];
                    //objMaterial.Material_id = dr[""];
                    lsMaterial.Add(objMaterial);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lsMaterial;
        }

        public List<MaterialBE> buscarMateriales(string pNombre)
        {
            List<MaterialBE> lsMaterial = new List<MaterialBE>();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from products where ProductName like '%" + pNombre + "%'", this.ConnectionString);
                DataSet ds = new DataSet();
                da.Fill(ds, "products");
                DataTable dt = ds.Tables[0];

                foreach (DataRow dr in dt.Rows)
                {
                    MaterialBE objMaterial = new MaterialBE();
                    objMaterial.CodMaterial = dr["ProductID"].ToString();
                    objMaterial.DescMaterial = dr["ProductName"].ToString();
                    objMaterial.PrecioUnit = Convert.ToDecimal(dr["UnitPrice"]);
                    objMaterial.CodUMedida = dr["QuantityPerUnit"].ToString();
                    //objMaterial. = dr[""];
                    //objMaterial.Material_id = dr[""];
                    lsMaterial.Add(objMaterial);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lsMaterial;
        }

        public MaterialBE leerMaterial(string pCodigo)
        {
            return this.leerMaterial(Convert.ToInt32(pCodigo));
        }

        public MaterialBE leerMaterial(int pCodigo)
        {
            MaterialBE objMaterial = null;
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("select * from products where ProductID = '" + pCodigo.ToString() + "'", this.ConnectionString);
                DataSet ds = new DataSet();
                da.Fill(ds, "products");
                DataTable dt = ds.Tables[0];

                if (dt.Rows.Count == 1)
                {
                    DataRow dr = dt.Rows[0];
                    objMaterial = new MaterialBE();
                    objMaterial.CodMaterial = dr["ProductID"].ToString();
                    objMaterial.DescMaterial = dr["ProductName"].ToString();
                    objMaterial.PrecioUnit = Convert.ToDecimal(dr["UnitPrice"]);
                    objMaterial.CodUMedida = dr["QuantityPerUnit"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return objMaterial;
        }

        #endregion

        #region IMaterialDALC Members


        public decimal getPrecio(string pCodMaterial, string pCodCliente)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IMaterialDALC Members


        public DateTime validaDespacho(string pCodCentro, int pCantidad, DateTime pFechaEntrega)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IMaterialDALC Members


        public string validaDespacho(out DateTime pFechaResult, string pCodCentro, int pCantidad, DateTime pFechaEntrega)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
